package com.hxd.otc.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.util.DensityUtil;

/**
 * 包  名 :  com.hxd.otc.widget
 * 类  名 :  RecyclerListDivider
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/6 11:47
 * 描  述 :  ${TODO}
 */
public class RecyclerListDivider extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int mDividerHeight = 2;


    /**
     * Default
     **/
    public RecyclerListDivider() {
        this(DensityUtil.dp2px((float) 0.5), 0xffDAE7FF);
    }

    public RecyclerListDivider(int color) {
        this(DensityUtil.dp2px((float) 0.5), color);
    }

    public RecyclerListDivider(int dividerHeight, int dividerColor) {
        mDividerHeight = dividerHeight;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mDividerHeight);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        final int left = parent.getPaddingLeft();
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize - 1; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin;
            final int bottom = top + mDividerHeight;
            if (mPaint != null) {
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }
}

